<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('parts.approval.quick Search Fields') })"
        ></TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef"></Table>

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import {forEach} from "lodash-es";
import {i18n} from "/@/lang";

defineOptions({
    name: 'parts/approval',
})

const { t } = useI18n()
const tableRef = ref()
//const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])
const optButtons: OptButton[] = [
  {
    render: 'tipButton',
    name: 'edit',
    title: 'Approval',
    text: '审核',
    type: 'primary',
    icon: 'fa fa-pencil',
    class: 'table-row-edit',
    disabledTip: false,
    display: (row: TableRow, field: TableColumn) => {
      return row.approval_status == 0;
    },
  },
]

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/parts.Approval/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('parts.approval.approval_type'), prop: 'approval_type', align: 'center', render: 'tag', operator: 'eq', sortable: false, replaceValue: { '0': t('parts.approval.approval_type 0'), '1': t('parts.approval.approval_type 1'), '2': t('parts.approval.approval_type 2'), '3': t('parts.approval.approval_type 3') } },
            { label: t('parts.approval.approval_status'), prop: 'approval_status', align: 'center', render: 'tag', operator: 'eq', sortable: false, replaceValue: { '0': t('parts.approval.approval_status 0'), '1': t('parts.approval.approval_status 1'), '2': t('parts.approval.approval_status 2') } },
            { label: t('parts.approval.admin__username'), prop: 'admin.username', align: 'center', operatorPlaceholder: t('Fuzzy query'), render: 'tags', operator: 'LIKE' },
            { label: t('parts.approval.create_time'), prop: 'create_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            { label: t('parts.approval.remark'), prop: 'remark', align: 'center', operator: false },
            { label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: [undefined],
    },
    {
        defaultItems: { id: 'none', approval_type: '0', approval_status: '0' },
        extend:{
          old:{
            id:"",
            parts_name:"",
            parts_code:"",
            parts_img:"",
            manufacturer_name:"",
            parts_cost:"",
            parts_money:"",
            entry_nur:"",
            entry_date:"",
            remark:""
          },
          new:{
            id:"",
            parts_name:"",
            parts_code:"",
            parts_img:"",
            manufacturer_name:"",
            parts_cost:"",
            parts_money:"",
            entry_nur:"",
            entry_date:"",
            remark:""
          },
          color:{
              id:"",
            parts_name:"",
            parts_code:"",
            parts_img:"",
            manufacturer_name:"",
            parts_cost:"",
            parts_money:"",
            entry_nur:"",
            entry_date:"",
            remark:""
          }
        }
    },{
      onTableAction(res):void
      {
        console.log(res);
        baTable.form.extend.color = {
          id:"",
          parts_name:"",
          parts_code:"",
          parts_img:"",
          manufacturer_name:"",
          parts_cost:"",
          parts_money:"",
          entry_nur:"",
          entry_date:"",
          remark:""
        }
        if(res.event == 'edit'){

          if(res.data.row.approval_type == "0" || res.data.row.approval_type == "1")
          {
            baTable.form.extend.new = JSON.parse(res.data.row.new_json);
            baTable.form.extend.old  = JSON.parse(res.data.row.old_json);

          }
          if(res.data.row.approval_type == "2" )
          {
            baTable.form.extend.new = JSON.parse(res.data.row.new_json);
            baTable.form.extend.old  = JSON.parse(res.data.row.old_json);
            baTable.form.extend.new.parts_name = JSON.parse(res.data.row.new_json).partsInfo.parts_name;
            baTable.form.extend.old.parts_name = JSON.parse(res.data.row.old_json).partsInfo.parts_name;

            baTable.form.extend.new.parts_code = JSON.parse(res.data.row.new_json).partsInfo.parts_code;
            baTable.form.extend.old.parts_code = JSON.parse(res.data.row.old_json).partsInfo.parts_code;

            baTable.form.extend.new.parts_img = JSON.parse(res.data.row.new_json).partsInfo.parts_img;
            baTable.form.extend.old.parts_img = JSON.parse(res.data.row.old_json).partsInfo.parts_img;

            baTable.form.extend.new.manufacturer_name = JSON.parse(res.data.row.new_json).manufacturerInfo.manufacturer_name;
            baTable.form.extend.old.manufacturer_name = JSON.parse(res.data.row.old_json).manufacturerInfo.manufacturer_name;
          }
          if(res.data.row.approval_type == "3")
          {
            baTable.form.extend.new = JSON.parse(res.data.row.new_json);
            baTable.form.extend.old  = JSON.parse(res.data.row.old_json);
            //baTable.form.extend.new.parts_name = JSON.parse(res.data.row.new_json).partsInfo.parts_name;
            baTable.form.extend.old.parts_name = JSON.parse(res.data.row.old_json).partsInfo.parts_name;

            //baTable.form.extend.new.parts_code = JSON.parse(res.data.row.new_json).partsInfo.parts_code;
            baTable.form.extend.old.parts_code = JSON.parse(res.data.row.old_json).partsInfo.parts_code;

            //baTable.form.extend.new.parts_img = JSON.parse(res.data.row.new_json).partsInfo.parts_img;
            baTable.form.extend.old.parts_img = JSON.parse(res.data.row.old_json).partsInfo.parts_img;

            //baTable.form.extend.new.manufacturer_name = JSON.parse(res.data.row.new_json).manufacturerInfo.manufacturer_name;
            baTable.form.extend.old.manufacturer_name = JSON.parse(res.data.row.old_json).manufacturerInfo.manufacturer_name;
          }


        }

        Object.keys(baTable.form.extend.new).forEach(function (key) {
          //设置无法匹配项颜色
          if (baTable.form.extend.new[key] != baTable.form.extend.old[key]) {
            baTable.form.extend.color[key] = 'background-color: red;';
          }
        })
      }
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss"></style>
